package com.tsugun.mediaplayer.view.activity;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.presenter.provider.ListProvider;
import com.tsugun.mediaplayer.view.fragment.SettingFragment;


import butterknife.BindView;
import butterknife.ButterKnife;

public class SettingActivity extends BaseActivity {
    // 标题颜色
    private static final int TITLE_TEXT_COLOR = 0xfffafafa;

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initView();
        initData();
        bindView();
    }

    private void initView() {
        startFragment(new SettingFragment());
    }

    private void initData() {
    }

    private void bindView() {
        mToolbar.setTitle(getString(R.string.str_nav_item_setting));
        mToolbar.setTitleTextColor(TITLE_TEXT_COLOR);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(getDrawable(R.drawable.ic_arrow_back));
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void onMusicLoadFailed() {

    }

    @Override
    protected void onMusicLoadComplete(ListProvider musicListProvider) {

    }

    @Override
    public void onSettingChanged() {
        // 当设置发生改变调用该方法
        setSettingTheme();
        checkNightMode();
    }


    @Override
    public void onBackPressed() {
        if (getMusicFragmentManager().getCount() > 1) {
            getMusicFragmentManager().backFragment();
            mToolbar.setTitle(getString(R.string.str_nav_item_setting));
            return;
        }
        super.onBackPressed();
        clearFragments();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 设置主题
     */
    private void setSettingTheme() {
        mToolbar.setBackgroundColor(getThemeColor());
    }

    /**
     * @param fragment 需要启动的fragment
     * @param itemName 启动的fragment的标题
     */
    public void startFragment(Fragment fragment, String itemName) {
        mToolbar.setTitle(itemName);
        startFragment(fragment);
    }
}
